/** 
 * File-Name:ArticleAction.java
 *
 * Created on 2011-6-2 下午10:10:52
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package site.web;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import neoutil.CommonAction;
import neoutil.CommonService;
import neoutil.FileUtils;
import neoutil.Page;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import site.entity.Article;
import site.entity.Category;
import site.entity.User;
import site.service.ArticleRepository;
import site.service.CategoryRepository;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-2 下午10:10:52
 */
@SuppressWarnings("serial")
public class ArticleAction extends CommonAction {
	private static Logger log = Logger.getLogger(ArticleAction.class);
	private Article article;
	private File articleImage;
	private String articleImageContentType;
	private String articleImageFileName;
	private String imgFile;
	private String imgFileContentType;
	private String imgFileFileName;
	private File articleFile;
	private String articleFileContentType;
	private String articleFileFileName;
	private String imageSavePath;
	private String fileSavePath;
	private Page<Article> page = new Page<Article>(15);
	private List<Article> hotArticls = null;
	private List<Category> categories;
	private int cid;
	private Category category;

	public String toAddArticle() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				int cid = Integer.parseInt(getRequestParameter("cid"));

				CategoryRepository categoryRepository = new CategoryRepository();
				category = categoryRepository.getCategoryByCid(cid);

				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}

	}

	public String addArticle() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				article.setAuthor(user);

				if (articleImage != null) {
					String imageName = article.getUuid()
							+ FileUtils.getExtention(getArticleImageFileName());
					FileUtils.write(getImageSavePath() + "\\", imageName,
							getArticleImage());

					article.setImagePath(imageName);
				}
				if (articleFile != null) {
					String fileName = article.getUuid()
							+ FileUtils.getExtention(getArticleFileFileName());
					FileUtils.write(getFileSavePath() + "\\", fileName,
							getArticleFile());

					article.setFilePath(fileName);
				}

				ArticleRepository articleRepository = new ArticleRepository();
				if (CommonService.SUCCESS == articleRepository.save(article)) {
					return SUCCESS;
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String downloadArticleEnclosure() {
		return SUCCESS;
	}

	public InputStream getEnclosureFile() {
		try {
			String uuid = getRequestParameter("uuid");
			ArticleRepository articleRepository = new ArticleRepository();
			article = articleRepository.getArticleByUuid(uuid);

			System.err
					.println(getFileSavePath() + "\\" + article.getFilePath());

			return new java.io.FileInputStream(getFileSavePath() + "\\"
					+ article.getFilePath());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String toEditArticle() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				String uuid = getRequestParameter("uuid");
				if (uuid == null || "".equals(uuid)) {
					return INPUT;
				} else {
					ArticleRepository articleRepository = new ArticleRepository();
					article = articleRepository.getArticleByUuid(uuid);
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	// public String editArticle() {
	// try {
	// User user = (User) getSessionObject("loginUser");
	// if (user == null) {
	// log.error("用户未登录！");
	// return RELOGIN;
	// } else {
	//
	// if (articleImage != null) {
	// String imageName = article.getUuid()
	// + FileUtils.getExtention(getArticleImageFileName());
	// FileUtils.write(getImageSavePath() + "\\", imageName,
	// getArticleImage());
	//
	// article.setImagePath(imageName);
	// }
	// if (articleFile != null) {
	// String fileName = article.getUuid()
	// + FileUtils.getExtention(getArticleFileFileName());
	// FileUtils.write(getFileSavePath() + "\\", fileName,
	// getArticleFile());
	//
	// article.setFilePath(fileName);
	// }
	//
	// ArticleRepository articleRepository = new ArticleRepository();
	// article.setAuthor(user);
	// if (CommonService.SUCCESS == articleRepository.update(article)) {
	// return SUCCESS;
	// } else {
	// return INPUT;
	// }
	// }
	// } catch (Exception e) {
	// e.printStackTrace();
	// return INPUT;
	// }
	// }

	public String deleteArticle() {
		try {
			User user = (User) getSessionObject("loginUser");
			if (user == null) {
				log.error("用户未登录！");
				return RELOGIN;
			} else {
				long id = Long.parseLong(getRequestParameter("id"));
				ArticleRepository articleRepository = new ArticleRepository();
				if (CommonService.SUCCESS == articleRepository.delete(id)) {
					return SUCCESS;
				} else {
					return INPUT;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String getArticleByUuid() {
		try {
			String uuid = getRequestParameter("uuid");
			if (uuid == null || "".equals(uuid)) {
				return INPUT;
			} else {
				ArticleRepository articleRepository = new ArticleRepository();
				article = articleRepository.getArticleByUuid(uuid);

				CategoryRepository categoryRepository = new CategoryRepository();
				category = categoryRepository
						.getCategoryByCid(article.getCid());
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String getArticlesByCid() {
		try {
			int cid = Integer.parseInt(getRequestParameter("cid"));
			ArticleRepository articleRepository = new ArticleRepository();
			page = articleRepository.getCheckedArticlesByCid(page, cid);

			CategoryRepository categoryRepository = new CategoryRepository();
			category = categoryRepository.getCategoryByCid(cid);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String getLatestArticleByCid() {
		try {
			int cid = Integer.parseInt(getRequestParameter("cid"));
			ArticleRepository articleRepository = new ArticleRepository();
			article = articleRepository.getLatestArticleByCid(cid);

			CategoryRepository categoryRepository = new CategoryRepository();
			category = categoryRepository.getCategoryByCid(article.getCid());

			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String searchArticles() {
		try {
			String query = getRequestParameter("query");
			if (query == null || "".equals(query)) {
				return INPUT;
			} else {
				System.err.println("Query:"
						+ new String(query.getBytes("iso-8859-1"), "UTF-8"));
				ArticleRepository articleRepository = new ArticleRepository();
				page = articleRepository.search(page,
						new String(query.getBytes("iso-8859-1"), "UTF-8"));
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}

	}

	/**
	 * Description:
	 * 
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Page<Article> getPage() {
		return page;
	}

	public void setPage(Page<Article> page) {
		this.page = page;
	}

	public File getArticleImage() {
		return articleImage;
	}

	public void setArticleImage(File articleImage) {
		this.articleImage = articleImage;
	}

	public String getArticleImageContentType() {
		return articleImageContentType;
	}

	public void setArticleImageContentType(String articleImageContentType) {
		this.articleImageContentType = articleImageContentType;
	}

	public String getArticleImageFileName() {
		return articleImageFileName;
	}

	public void setArticleImageFileName(String articleImageFileName) {
		this.articleImageFileName = articleImageFileName;
	}

	public File getArticleFile() {
		return articleFile;
	}

	public void setArticleFile(File articleFile) {
		this.articleFile = articleFile;
	}

	public String getArticleFileContentType() {
		return articleFileContentType;
	}

	public void setArticleFileContentType(String articleFileContentType) {
		this.articleFileContentType = articleFileContentType;
	}

	public String getArticleFileFileName() {
		return articleFileFileName;
	}

	public void setArticleFileFileName(String articleFileFileName) {
		this.articleFileFileName = articleFileFileName;
	}

	@SuppressWarnings("deprecation")
	public String getImageSavePath() {
		return ServletActionContext.getRequest().getRealPath(imageSavePath);
	}

	public void setImageSavePath(String imageSavePath) {
		this.imageSavePath = imageSavePath;
	}

	public String getFileSavePath() {
		return ServletActionContext.getRequest().getRealPath(fileSavePath);
	}

	public void setFileSavePath(String fileSavePath) {
		this.fileSavePath = fileSavePath;
	}

	public String getImgFile() {
		return imgFile;
	}

	public void setImgFile(String imgFile) {
		this.imgFile = imgFile;
	}

	public String getImgFileContentType() {
		return imgFileContentType;
	}

	public void setImgFileContentType(String imgFileContentType) {
		this.imgFileContentType = imgFileContentType;
	}

	public String getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public List<Article> getHotArticls() {
		return hotArticls;
	}

	public void setHotArticls(List<Article> hotArticls) {
		this.hotArticls = hotArticls;
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

}
